package com.zjl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjl.common.CustomException;
import com.zjl.dto.SetmealDto;
import com.zjl.entity.Category;
import com.zjl.entity.Setmeal;
import com.zjl.entity.SetmealDish;
import com.zjl.mapper.SetmealMapper;
import com.zjl.service.SetmealDishService;
import com.zjl.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐信息，同时新增套餐对应的SetmealDish数据
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithSetmealDish(SetmealDto setmealDto) {
        //保存setmeal对象基本信息
        this.save(setmealDto);

        //取出setmealDishes列表，为每一个对象手动设置setmealId，从前端获取时它们并没有setmealId
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }

        //在setmealDish表中批次新增数据
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 套餐信息分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<Setmeal> getPage(Integer page, Integer pageSize, String name) {
        //分页构造器
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);

        //条件构造器
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        //添加条件
            //若name参数有值，根据name查询
        wrapper.like((name!=null&&name!=""),Setmeal::getName,name);
            //根据操作时间逆序排
        wrapper.orderByDesc(Setmeal::getUpdateTime);

        //进行查询
        this.page(setmealPage,wrapper);

        return setmealPage;
    }

    /**
     * 删除套餐信息，并删除SetmealDish表中于当前套餐关联的数据
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithSetmealDish(List<Long> ids) {
        //查询当前ids中的setmeal套餐状态，若存在售卖中（status=1）的套餐，则无法删除，并抛出业务异常
        LambdaQueryWrapper<Setmeal> SetmealWrapper = new LambdaQueryWrapper<>();
        SetmealWrapper.in(Setmeal::getId,ids);
        SetmealWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(SetmealWrapper);
        if (count>0){
            throw new CustomException("存在有售卖中的套餐，无法删除");
        }


        //删除套餐信息
        this.removeByIds(ids);

        //删除SetmealDish表中与当前套餐们中关联的数据
        //delete from setmeal_dish where setmeal_dish.setmeal_id in (ids);
        LambdaQueryWrapper<SetmealDish> setmealDishWrapper = new LambdaQueryWrapper<>();
        setmealDishWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(setmealDishWrapper);

    }

    /**
     * 根据条件查询套餐
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> getByConditions(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,1);
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        return this.list(queryWrapper);
    }


}
